﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZeCloud.Resto55.Business;
using ZeCloud.Resto55.Business.Table;

namespace Resto55Business_WebRole
{
    public partial class Consult : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request.QueryString["id"]))
                Response.Redirect("/");
        }

        protected void HlAddE_Click(object sender, EventArgs e)
        {
            PAddE.Visible = true;
            ((LinkButton)sender).Visible = false;
        }

        protected void Bt_Add_Click(object sender, EventArgs e)
        {
            RestoService RestoServ = BusinessFacade.CreerFacade().GetRestoService();
            //ajout d'un element
            Element el =RestoServ.CreerElement(Request.QueryString["id"], T_El.Text, T_Cat.Text, T_Descr.Text);
            //ajout du prix
            double prix;
            if (!string.IsNullOrEmpty(Tb_Price.Text) && double.TryParse(Tb_Price.Text,out prix))
            {
                RestoServ.AjouterPrix(el.GetElementID(), prix);
            }
            PAddE.Visible = false;
            HlAddE.Visible = true;
            GridView1.DataBind();
        }

        protected void HyperLink2_DataBinding(object sender, EventArgs e)
        {
            
        }

        protected void Label2_DataBinding(object sender, EventArgs e)
        {
            RestoService RestoServ = BusinessFacade.CreerFacade().GetRestoService();
            DetailPrix dprix=RestoServ.RechercherDernierPrixPourElement(Eval("PartitionKey") + "_" + Eval("RowKey"));
            if(dprix!=null)
            {
                Label lb= ((Label)sender); 
                ((HyperLink)(lb.NamingContainer.FindControl("Hl_Price")))
                    .NavigateUrl = "Price.aspx?id=" + Eval("PartitionKey") +"_" + Eval("RowKey");
                lb.Text = dprix.prix.ToString() + " €";
            }
        }

        protected void Link_add_Click(object sender, EventArgs e)
        {
            LinkButton lb=((LinkButton)sender);   
            lb.NamingContainer.FindControl("P_AddPrice").Visible = true;
            lb.Visible = false;

        }
        /// <summary>
        /// recupére le prix saisi et l'ajoute à l'élément 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Bt_SavePr_Click(object sender, EventArgs e)
        {
            Button bt = ((Button)sender);
            RestoService RestoServ = BusinessFacade.CreerFacade().GetRestoService();
            string ElId=((HiddenField)bt.NamingContainer.FindControl("HF_ElId")).Value; 
            double Price= double.Parse(((TextBox)bt.NamingContainer.FindControl("TB_Price2")).Text);
            RestoServ.AjouterPrix(ElId,Price);
            bt.NamingContainer.FindControl("Link_add").Visible = true;
            bt.NamingContainer.Visible=false;
            GridView1.DataBind();
            
        }

        protected void L_Name_Load(object sender, EventArgs e)
        {
            RestoService RestoServ = BusinessFacade.CreerFacade().GetRestoService();
            ((Label)sender).Text= RestoServ.GetRestoByUniqueId(Request.QueryString["id"]).nom ;

        }

        protected void Bt_Cancel_Click(object sender, EventArgs e)
        {
            PAddE.Visible = false;
            HlAddE.Visible = true;
        }

       
    }
}
